package com.jhgsys.internal.hazard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.accident.entity.HazardExamineAuditRecord;
import com.jhgsys.internal.accident.entity.HazardExamineFile;
import com.jhgsys.internal.accident.entity.HazardExamineRectificationReturnRecord;
import com.jhgsys.internal.accident.service.IHazardExamineAuditRecordService;
import com.jhgsys.internal.accident.service.IHazardExamineFileService;
import com.jhgsys.internal.accident.service.IHazardExamineRectificationReturnRecordService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.dto.HazardExamineRecordDto;
import com.jhgsys.internal.hazard.entity.HazardExamineRecord;
import com.jhgsys.internal.hazard.mapper.HazardExamineRecordMapper;
import com.jhgsys.internal.hazard.service.IHazardExamineRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 *  Service实现
 *
 * @author admin
 * @date 2024-08-20 15:49:12
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardExamineRecordServiceImpl extends ServiceImpl<HazardExamineRecordMapper, HazardExamineRecord> implements IHazardExamineRecordService {

    @Autowired
    private IHazardExamineFileService hazardExamineFileService;

    @Autowired
    private IHazardExamineAuditRecordService hazardExamineAuditRecordService;

    @Autowired
    private IHazardExamineRectificationReturnRecordService hazardExamineRectificationReturnRecordService;

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-20 15:49:12
     * @param request QueryRequest
     * @param hazardExamineRecord hazardExamineRecord
     * @return IPage<HazardExamineRecord>
    */
    @Override
    public IPage<HazardExamineRecord> findHazardExamineRecordPage(QueryRequest request, HazardExamineRecord hazardExamineRecord) {
        LambdaQueryWrapper<HazardExamineRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineRecord::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardExamineRecord> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-20 15:49:12
     * @param hazardExamineRecord hazardExamineRecord
     * @return List<HazardExamineRecord>
     */
    @Override
    public List<HazardExamineRecord> findHazardExamineRecordList(HazardExamineRecord hazardExamineRecord) {
	    LambdaQueryWrapper<HazardExamineRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineRecord::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 安全检查录入 以HazardExamine为主表查询（所有）
     * @author admin
     * @date 2024-08-20 15:49:12
     * @param hazardExamineRecordDto hazardExamineRecordDto
     * @return List<HazardExamineRecordDto>
     */
    @Override
    public Map<String,Object> findHazardExamineRecordListWithExamines(HazardExamineRecordDto hazardExamineRecordDto) {
        Map<String,Object> result = new HashMap<>();
        List<HazardExamineRecordDto> list = this.baseMapper.selectListWithExamines(hazardExamineRecordDto);
        result.put("rows",list);
        result.put("total",list.size());
		return result;
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-08-20 15:49:12
    * @param id
    * @return HazardExamineRecord
    */
    @Override
    public HazardExamineRecordDto findHazardExamineRecordById(String id){
        HazardExamineRecordDto hazardExamineRecordDto = new HazardExamineRecordDto();
        HazardExamineRecord hazardExamineRecord = this.baseMapper.selectById(id);
        if(null != hazardExamineRecord){
            hazardExamineRecordDto = BeanHelper.copyProperties(hazardExamineRecord,HazardExamineRecordDto.class);
        }
        //检查记录问题附件
        hazardExamineRecordDto.setExamineFileList(hazardExamineFileService.list(new LambdaQueryWrapper<HazardExamineFile>()
                .eq(HazardExamineFile::getExamineRecordId,hazardExamineRecordDto.getExamineRecordId())
                .eq(HazardExamineFile::getExamineFileType,"1")
        ));
        //整改附件
        hazardExamineRecordDto.setRectifyFileList(hazardExamineFileService.list(new LambdaQueryWrapper<HazardExamineFile>()
                .eq(HazardExamineFile::getExamineRecordId,hazardExamineRecordDto.getExamineRecordId())
                .eq(HazardExamineFile::getExamineFileType,"2")
        ));
        //整改退回记录
        hazardExamineRecordDto.setRectifyReturnRecordList(hazardExamineRectificationReturnRecordService.list(new LambdaQueryWrapper<HazardExamineRectificationReturnRecord>()
                .eq(HazardExamineRectificationReturnRecord::getExamineRecordId,hazardExamineRecordDto.getExamineRecordId())
                .orderByDesc(HazardExamineRectificationReturnRecord::getCreatedTime)
        ));
        //审核记录
        hazardExamineRecordDto.setAuditRecordList(hazardExamineAuditRecordService.list(new LambdaQueryWrapper<HazardExamineAuditRecord>()
                .eq(HazardExamineAuditRecord::getExamineRecordId,hazardExamineRecordDto.getExamineRecordId())
                .orderByDesc(HazardExamineAuditRecord::getCreatedTime)
        ));
        return hazardExamineRecordDto;
    }

    /**
     * 新增
     * @author admin
     * @date 2024-08-20 15:49:12
     * @param hazardExamineRecord hazardExamineRecord
     */
    @Override
    @Transactional
    public void createHazardExamineRecord(HazardExamineRecord hazardExamineRecord) {
        hazardExamineRecord.setCreatedTime(new Date());
        this.save(hazardExamineRecord);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-08-20 15:49:12
     * @param hazardExamineRecord hazardExamineRecord
     */
    @Override
    @Transactional
    public void updateHazardExamineRecord(HazardExamineRecord hazardExamineRecord) {
        hazardExamineRecord.setUpdatedTime(new Date());
        this.saveOrUpdate(hazardExamineRecord);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-08-20 15:49:12
     * @param hazardExamineRecord hazardExamineRecord
     * @param hazardExamineRecordIds hazardExamineRecordIds
     */
    @Override
    @Transactional
    public void deleteHazardExamineRecord(HazardExamineRecord hazardExamineRecord,String hazardExamineRecordIds) {
        List<String> list = Arrays.asList(hazardExamineRecordIds.split(StringPool.COMMA));
        this.baseMapper.update(hazardExamineRecord,new LambdaQueryWrapper<HazardExamineRecord>().in(HazardExamineRecord::getExamineRecordId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-08-20 15:49:12
    * @param hazardExamineRecordIds hazardExamineRecordIds
    */
    @Override
    @Transactional
    public void physicallyDeleteHazardExamineRecord(String hazardExamineRecordIds) {
        List<String> list = Arrays.asList(hazardExamineRecordIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardExamineRecord> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardExamineRecord::getExamineRecordId,list);
        this.remove(wapper);
    }

    /**
     * 根据安全检查examine_id查询
     * @author admin
     * @param examineIds
     * @return List<HazardExamineRecordDto>
     */
    @Override
    public List<HazardExamineRecordDto> findByExamineIds(List<String> examineIds) {
        List<HazardExamineRecordDto> result = new ArrayList<>();
        LambdaQueryWrapper<HazardExamineRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineRecord::getDeletedFlg, FebsConstant.VALID);
        queryWrapper.in(HazardExamineRecord::getExamineId,examineIds);
        List<HazardExamineRecord> hazardExamineRecords = this.baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(hazardExamineRecords)){
            result = BeanHelper.copyWithCollection(hazardExamineRecords,HazardExamineRecordDto.class);
        }
        return result;
    }

}
